//
//  AppDelegate.swift
//  Weibo
//
//  Created by Apple on 16/11/12.
//  Copyright © 2016年 Wbw. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //实例化window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //设置根视图控制器
//        window?.rootViewController = WBMainViewController()
        //未登录的时候不能显示欢迎界面
//        window?.rootViewController = WBWeicomeViewController()
        //未登录时显示访客界面,登录后显示欢迎界面
        window?.rootViewController = WBUserAccountViewModel.shared.userLogin ? WBWeicomeViewController() : WBMainViewController()
        
        //注册通知
        registerNotification()
        
        //window可见
        window?.makeKeyAndVisible()
        return true
    }

    //注册通知方法
    private func registerNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController(n:)), name: NSNotification.Name(kChoseRootViewController), object: nil)
    }
    //监听方法
    @objc private func switchRootViewController(n:Notification) {
        //根据通知对象中object来判断显示什么控制器 如果是nil发送过来的,说明是欢迎界面发送的消息,切换到主界面 如果不是 就切换到欢迎界面
        let vc = n.object == nil ? WBMainViewController() : WBWeicomeViewController()
        //设置跟视图控制器
        window?.rootViewController = vc
    }
    //移除通知: 规范
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
}

